package org.lease.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.lease.mapper.GraphInfoMapper;
import org.lease.mapper.LeaseAgreementMapper;
import org.lease.model.dto.LeaseAgreementDTO;
import org.lease.model.entity.*;
import org.lease.model.enums.ItemType;
import org.lease.model.enums.LeaseStatus;
import org.lease.model.query.LeaseAgreementPageQuery;
import org.lease.model.vo.AgreementDetailVo;
import org.lease.model.vo.AgreementItemVo;
import org.lease.model.vo.GraphVo;
import org.lease.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement> implements LeaseAgreementService {
    @Lazy
    @Autowired
    private ApartmentInfoService apartmentInfoService;
    @Autowired
    private RoomInfoService roomInfoService;
    @Autowired
    private PaymentTypeService paymentTypeService;
    @Autowired
    private LeaseTermService leaseTermService;

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    // 统计在租的房间总数
    @Override
    public Long countLeasedByApartmentId(Long apartmentId) {
        LambdaQueryWrapper<LeaseAgreement> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LeaseAgreement::getApartmentId, apartmentId);
        wrapper.in(LeaseAgreement::getStatus, LeaseStatus.SIGNED, LeaseStatus.WITHDRAWING);
        return count(wrapper);
    }

    // 分页条件查询租约信息
    @Override
    public IPage<LeaseAgreementDTO> pageQueryLeaseAgreementDTO(LeaseAgreementPageQuery query) {
        Page<LeaseAgreementDTO> page = new Page<>(query.getCurrent(), query.getSize());
        return baseMapper.pageQueryLeaseAgreementDTO(page, query);
    }

    // 根据 ID 查询租约的详细信息
    @Override
    public LeaseAgreementDTO getLeaseAgreementDTOById(Long id) {
        // 1.查询租约信息
        LeaseAgreement leaseAgreement = baseMapper.selectById(id);
        // 2.查询公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoService.getById(leaseAgreement.getApartmentId());
        // 3.查询房间信息
        RoomInfo roomInfo = roomInfoService.getById(leaseAgreement.getRoomId());
        // 4.查询支付方式
        PaymentType paymentType = paymentTypeService.getById(leaseAgreement.getPaymentTypeId());
        // 5.查询租期
        LeaseTerm leaseTerm = leaseTermService.getById(leaseAgreement.getLeaseTermId());

        LeaseAgreementDTO dto = new LeaseAgreementDTO();
        BeanUtils.copyProperties(leaseAgreement, dto);
        dto.setApartmentInfo(apartmentInfo);
        dto.setRoomInfo(roomInfo);
        dto.setPaymentType(paymentType);
        dto.setLeaseTerm(leaseTerm);

        return dto;
    }

    @Override
    public List<AgreementItemVo> listItem(String username) {
        return baseMapper.listItem(username);
    }

    @Override
    public AgreementDetailVo getDetailById(Long id) {

        // 1.查询租约信息
        LeaseAgreement leaseAgreement = baseMapper.selectById(id);
        if (leaseAgreement == null) {
            return null;
        }
        // 2.查询公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoService.getById(leaseAgreement.getApartmentId());

        // 3.查询房间信息
        RoomInfo roomInfo = roomInfoService.getById(leaseAgreement.getRoomId());

        // 4.查询图片信息
        List<GraphVo> roomGraphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.ROOM, leaseAgreement.getRoomId());
        List<GraphVo> apartmentGraphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.APARTMENT, leaseAgreement.getApartmentId());

        // 5.查询支付方式
        PaymentType paymentType = paymentTypeService.getById(leaseAgreement.getPaymentTypeId());

        // 6.查询租期
        LeaseTerm leaseTerm = leaseTermService.getById(leaseAgreement.getLeaseTermId());

        AgreementDetailVo agreementDetailVo = new AgreementDetailVo();
        BeanUtils.copyProperties(leaseAgreement, agreementDetailVo);
        agreementDetailVo.setApartmentName(apartmentInfo.getName());
        agreementDetailVo.setRoomNumber(roomInfo.getRoomNumber());
        agreementDetailVo.setApartmentGraphVoList(apartmentGraphVoList);
        agreementDetailVo.setRoomGraphVoList(roomGraphVoList);
        agreementDetailVo.setPaymentTypeName(paymentType.getName());
        agreementDetailVo.setLeaseTermMonthCount(leaseTerm.getMonthCount());
        agreementDetailVo.setLeaseTermUnit(leaseTerm.getUnit());

        return agreementDetailVo;
    }

    @Override
    public List<LeaseTerm> listByRoomId(Long id) {
        return leaseTermService.listByRoomId(id);
    }
}
